﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CommonLibrary.DB;
using System.Data;
using CommonLibrary.DataModel.Basic;

namespace ServiceApplication.Dao.Basic
{
    public class FolderDao
    {
        FileDao fileDao = new FileDao();

        /// <summary>
        ///  添加文件夹
        /// </summary>
        /// <param name="folderName"></param>
        /// <param name="createTime"></param>
        /// <param name="userID"></param>
        /// <param name="folderPID"></param>
        /// <returns></returns>
        public Boolean AddFolder(String folderName, String createTime, String userID, String folderPID)
        {
            if (folderExist(folderName, folderPID, userID))
                return false;

            String sql = "INSERT INTO user_folder ([folder_pid],[folder_name],[folder_ctime],[folder_utime],[user_id]) values " +
                "(@folder_pid,@folder_name,@folder_ctime,@folder_utime,@user_id)";
            List<SqlParameter> parameterList = new List<SqlParameter>();

            parameterList.Add(new SqlParameter("@folder_pid", folderPID));
            parameterList.Add(new SqlParameter("@folder_name", folderName));
            parameterList.Add(new SqlParameter("@folder_ctime", createTime));
            parameterList.Add(new SqlParameter("@folder_utime", createTime));
            parameterList.Add(new SqlParameter("@user_id", userID));
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());

            return true;
        }

        private Boolean folderExist(String folderName, String folderPID, String userID)
        {
            String sql = "SELECT [folder_id],[folder_pid],[folder_name],[folder_ctime],[folder_utime],[user_id]" +
                "FROM user_folder WHERE [folder_PID] = @folderPID AND [folder_name]=@folderName AND [user_id]=@userID";

            List<SqlParameter> parameterList = new List<SqlParameter>();

            parameterList.Add(new SqlParameter("@folderPID", folderPID));
            parameterList.Add(new SqlParameter("@folderName", folderName));
            parameterList.Add(new SqlParameter("@userID", userID));

            var result = DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
            if (result > 0)
                return true;
            else
                return false;
        }

        private Boolean folderExist(String folderID)
        {
            String sql = "SELECT [folder_id],[folder_pid],[folder_name],[folder_ctime],[folder_utime],[user_id] FROM user_folder WHERE [folder_id]=@folderID";
            List<SqlParameter> parameterList = new List<SqlParameter>();

            parameterList.Add(new SqlParameter("@folderID", folderID));

            var result = DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
            if (result > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        ///  修改文件夹
        /// </summary>
        /// <param name="folderID"></param>
        /// <param name="folderName"></param>
        /// <param name="folderPID"></param>
        public Boolean ChangeFolder(String folderID, String folderName, String updateTime, String folderPID, String userID)
        {
            if (folderExist(folderName, folderPID, userID))
                return false;

            String sql = "UPDATE user_folder SET [folder_name]=@folderName,[folder_utime]=@folderUtime,[folder_PID] = @folderPID WHERE [folder_id]=@folderID";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@folderID", folderID));
            parameterList.Add(new SqlParameter("@folderName", folderName));
            parameterList.Add(new SqlParameter("@folderUtime", updateTime));
            parameterList.Add(new SqlParameter("@folderPID", folderPID));

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());

            return true;
        }

        /// <summary>
        ///  删除文件夹
        /// </summary>
        /// <param name="folderID"></param>
        /// <param name="delAll">是否强制删除</param>
        /// <returns></returns>
        public Boolean DeleteFolder(String folderID, String baseUrl, Boolean force)
        {
            if (folderExist(folderID))
            {
                var childFolders = childfolder(folderID);
                var childFiles = fileDao.GetFiles(folderID);
                if (force)
                {
                    fileDao.DeleteFiles(childFiles, baseUrl);
                    foreach (DataRow item in childFolders.Rows)
                    {
                        DeleteFolder(item["folder_id"].ToString(), baseUrl, force);
                        deleteFolder(folderID);
                    }
                    deleteFolder(folderID);
                    return true;
                }
                else
                {
                    if (childFolders.Rows.Count > 0 || childFiles.Rows.Count > 0)
                        return false;
                    else
                    {
                        deleteFolder(folderID);
                        return true;
                    }
                }
            }
            else
                return false;
        }

        public DataTable childfolder(String folderPID)
        {
            String sql = "SELECT [folder_id],[folder_pid],[folder_name],[folder_ctime],[folder_utime],[user_id] FROM user_folder WHERE [folder_pid]=@folderPID";

            List<SqlParameter> parameterList = new List<SqlParameter>();

            parameterList.Add(new SqlParameter("@folderPID", folderPID));

            return DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
        }

        private void deleteFolder(String folderID)
        {
            String sql = "DELETE FROM user_folder WHERE[folder_id]=@folderID";
            List<SqlParameter> parameterList = new List<SqlParameter>();

            parameterList.Add(new SqlParameter("@folderID", folderID));

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        /// <summary>
        ///  获得用户文件夹
        /// </summary>
        /// <param name="folderPID"></param>
        /// <param name="userID"></param>
        /// <param name="folderType"></param>
        /// <returns></returns>
        public FolderList GetFolders(String folderPID, String userID, FolderType folderType)
        {
            FolderList folderList = new FolderList();
            folderList.Folders = getFolders(folderPID, userID);
            if (folderType == FolderType.All)
            {
                folderList.Files = fileDao.GetFileList(folderPID, userID);
            }

            return folderList;
        }

        private List<FolderEntity> getFolders(String folderPID, String userID)
        {
            String sql = "SELECT [folder_id],[folder_pid],[folder_name],[folder_ctime],[folder_utime],[user_id]" +
                "FROM user_folder WHERE [folder_PID] = @folderPID AND [user_id]=@user_id";

            List<SqlParameter> parameterList = new List<SqlParameter>();

            parameterList.Add(new SqlParameter("@folderPID", folderPID));
            parameterList.Add(new SqlParameter("@user_id", userID));
            var result = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());


            List<FolderEntity> folderList = new List<FolderEntity>();
            foreach (DataRow item in result.Rows)
            {
                folderList.Add(new FolderEntity
                {
                    FolderID = item["folder_id"].ToString(),
                    FolderName = item["folder_name"].ToString(),
                    FolderPID = item["folder_PID"].ToString(),
                    CreateTime = item["folder_ctime"].ToString(),
                    UpdateTime = item["folder_utime"].ToString(),
                    UserID = item["user_id"].ToString()
                });
            }

            return folderList;
        }

        public List<FolderEntity> getFolders4Fuzzy(String folderName, String userID)
        {
            folderName = "%" + folderName + "%";
            String sql = "SELECT [folder_id],[folder_pid],[folder_name],[folder_ctime],[folder_utime],[user_id]" +
                "FROM user_folder WHERE [folder_name] like @folderName AND [user_id]=@user_id";

            List<SqlParameter> parameterList = new List<SqlParameter>();

            parameterList.Add(new SqlParameter("@folderName", folderName));
            parameterList.Add(new SqlParameter("@user_id", userID));
            var result = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());


            List<FolderEntity> folderList = new List<FolderEntity>();
            foreach (DataRow item in result.Rows)
            {
                folderList.Add(new FolderEntity
                {
                    FolderID = item["folder_id"].ToString(),
                    FolderName = item["folder_name"].ToString(),
                    FolderPID = item["folder_PID"].ToString(),
                    CreateTime = item["folder_ctime"].ToString(),
                    UpdateTime = item["folder_utime"].ToString(),
                    UserID = item["user_id"].ToString()
                });
            }

            return folderList;
        }

        public DataTable GetFolderNodes(String userID)
        {
            String sql = "SELECT [folder_id],[folder_pid],[folder_name],[folder_ctime],[folder_utime],[user_id]" +
                "FROM user_folder WHERE [user_id]=@user_id";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", userID));

            return DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
        }

        public DataTable GetFoldersByUserId(int user_id)
        {
            String sql = " SELECT folder_id 编码,folder_pid 上级编码,folder_name 名称,folder_ctime 创建时间,folder_utime 跟新时间,user_id 用户编码" +
                              " FROM user_folder WHERE user_id=@user_id ";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", user_id));

            return DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
        }

        public Boolean MoveFolder(String folderID, String folderPID, String userID)
        {
            if (isChildFolder(folderID, folderPID, userID))
                return false;
            else
            {
                String sql = "UPDATE user_folder SET [folder_pid]=@folderPID WHERE [folder_id]=@folderID";
                List<SqlParameter> parameterList = new List<SqlParameter>();
                parameterList.Add(new SqlParameter("@folderPID", folderPID));
                parameterList.Add(new SqlParameter("@folderID", folderID));
                DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
                return true;
            }
        }

        private Boolean isChildFolder(String folderID, String folderPID, String userID)
        {
            var folders = getFolders(folderID, userID);
            foreach (var item in folders)
            {
                if (item.FolderID == folderPID)
                    return true;
                else
                {
                    isChildFolder(item.FolderID, folderPID, userID);
                }
            }
            return false;
        }

        public Boolean RenameFolder(String folderID, String folderPID, String rename, String userID)
        {
            var folders = getFolders(folderPID, userID);
            foreach (var item in folders)
            {
                if (item.FolderName == rename)
                    return false;
            }

            String sql = "UPDATE user_folder SET [folder_name]=@folderName WHERE [folder_id]=@folderID";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@folderName", rename));
            parameterList.Add(new SqlParameter("@folderID", folderID));
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
            return true;
        }
    }

    public enum FolderType
    {
        All = 1,

        FolderOnly
    };
}
